﻿package labox.innovation.gameserver.model.actor.position;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import javolution.util.FastList;

import labox.innovation.gameserver.model.actor.L2Character;
import labox.innovation.gameserver.model.listener.LocationListener;
import labox.innovation.util.Point2D;

/**
 * 角色位置处理类
 */
public class CharPosition extends ObjectPosition {

	/**
	 * 上次坐标变化记录器
	 */
	protected Point2D _preWorldPosition;

	/**
	 * 坐标变化监听器
	 */
	protected List<LocationListener> _locationListener;

	// =========================================================
	// Constructor
	public CharPosition(L2Character activeObject) {
		super(activeObject);
		_preWorldPosition = new Point2D(0, 0);
	}

	public L2Character getActiveObject() {
		return (L2Character) _activeObject;
	}

	@Override
	protected void badCoords() {
		getActiveObject().decayMe();
	}

	/** Return the x position of the L2Object. */
	public final int getPreX() {
		return _preWorldPosition.getX();
	}

	/** Return the y position of the L2Object. */
	public final int getPreY() {
		return _preWorldPosition.getY();
	}

	public final boolean equalPreScenePosition(Point2D point) {
		return _preWorldPosition.equals(point);
	}

	public final void setPreScenePosition(Point2D point) {
		_preWorldPosition.setTo(point);
		if (_locationListener != null) {
			Iterator<LocationListener> it = _locationListener.iterator();
			while (it.hasNext()) {
				it.next().Location(getActiveObject().getObjectId(), point.getX(), point.getY());
			}
		}
	}

	public final void setPreScenePosition(int x, int y) {
		_preWorldPosition.setXY(x, y);
		if (_locationListener != null) {
			Iterator<LocationListener> it = _locationListener.iterator();
			while (it.hasNext()) {
				it.next().Location(getActiveObject().getObjectId(), x, y);
			}
		}
	}

	/**
	 * 增加坐标变化监听器
	 * 
	 * @param listener
	 */
	public void addLocationListener(LocationListener listener) {
		if (_locationListener == null)
			_locationListener = FastList.newInstance();
		_locationListener.add(listener);
	}

	/**
	 * 删除坐标变化监听器
	 * 
	 * @param listener
	 */
	public void removeLocationListener(LocationListener listener) {
		if (_locationListener == null)
			return;
		_locationListener.remove(listener);
	}
}
